<template>
  <div class="person">
    <h1>情况四：监视【ref】或者【reactive】定义的【对象类型】中的某个属性</h1>
    <h2>姓名：{{ person.name }}</h2>
    <h2>年龄：{{ person.age }}</h2>
    <h2>汽车：{{ person.car.c1 }}、{{ person.car.c2 }}</h2>
    
    <button @click="changeName">修改名字</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changeCar1">修改第一台车子</button>
    <button @click="changeCar2">修改第二台车子</button>
    <button @click="changeCar">修改整个车</button>

  </div>
</template>

<script lang="ts" setup name="Person">
  import { reactive,watch } from 'vue';
  //数据
  let person = reactive({
    name:'张三',
    age:18,
    car:{
      c1:'保时捷',
      c2:'迈巴赫'
    }
  });

  //方法
  function changeName(){
    person.name += '~';
    
  }
  function changeAge() {
    person.age += 1
  }
  function changeCar() {
    person.car = {c1:'雅迪',c2:'艾玛'};
  }
  function changeCar1() {
    person.car.c1 = '奥迪';
  }
  function changeCar2() {
    person.car.c2 = '大众';
  }

  //监视：watch情况四,将person.name变为一个返回的值（watch才能监视）
  watch(()=>(person.name), (newValue,oldValue) => { 
    console.log('person对象中姓名被修改了')
    console.log(newValue,oldValue);
})

  //监视：car对象内c1、c2属性的变化
//   watch(person.car, (newValue,oldValue) => { 
//     console.log('person对象中车被修改了')
//     console.log(newValue,oldValue);
// })

  //监视：整个car对象的变化:将person.car变为一个返回值
  watch(()=>person.car, (newValue,oldValue) => { 
    console.log('person对象中车被修改了')
    console.log(newValue,oldValue);
})
</script>
<style scoped>
  .person {
    background-color: skyblue;
    box-shadow: 0 0 10px;
    border-radius: 10px;
    padding: 20px;
  }
  button {
    margin: 0 5px;
  }
  li {
    font-size: 20px;
  }
</style>